package com.stylefeng.guns.modular.poemspoetry.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.poemspoetry.model.Poems;
import com.stylefeng.guns.modular.poemspoetry.service.IPoemsService;

/**
 * 唐诗控制器
 *
 * @author fengshuonan
 * @Date 2018-03-20 15:56:09
 */
@Controller
@RequestMapping("/poems")
public class PoemsController extends BaseController {

    private String PREFIX = "/poemspoetry/poems/";

    @Autowired
    private IPoemsService poemsService;

    /**
     * 跳转到唐诗首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "poems.html";
    }

    /**
     * 跳转到添加唐诗
     */
    @RequestMapping("/poems_add")
    public String poemsAdd() {
        return PREFIX + "poems_add.html";
    }

    /**
     * 跳转到修改唐诗
     */
    @RequestMapping("/poems_update/{poemsId}")
    public String poemsUpdate(@PathVariable Long poemsId, Model model) {
        Poems poems = poemsService.selectById(poemsId);
        model.addAttribute("item",poems);
        LogObjectHolder.me().set(poems);
        return PREFIX + "poems_edit.html";
    }

    /**
     * 获取唐诗列表
     */
    /*
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return poemsService.selectList(null);
    }
    */

     /**
     * 获取唐诗列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<Poems> page = new PageFactory<Poems>().defaultPage();
        EntityWrapper<Poems> ew = new EntityWrapper<Poems>();
         /*if (ToolUtil.isNotEmpty(condition)) {
                ew.eq("condition", condition);
         }*/
        return super.packForBT(poemsService.selectPage(page,ew));
    }

    /**
     * 新增唐诗
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Poems poems) {
        poemsService.insert(poems);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除唐诗
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long poemsId) {
        poemsService.deleteById(poemsId);
        return SUCCESS_TIP;
    }

    /**
     * 修改唐诗
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Poems poems) {
        poemsService.updateById(poems);
        return super.SUCCESS_TIP;
    }

    /**
     * 唐诗详情
     */
    @RequestMapping(value = "/detail/{poemsId}")
    @ResponseBody
    public Object detail(@PathVariable("poemsId") Long poemsId) {
        return poemsService.selectById(poemsId);
    }
}
